package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PdfUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysUserExportVo;
import com.triones.ljr.entity.dto.BannerDO;
import com.triones.ljr.entity.dto.ComplaintCategoryDO;
import com.triones.ljr.entity.dto.ComplaintDO;
import com.triones.ljr.entity.vo.*;
import com.triones.ljr.service.ComplaintService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * 我要投诉
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/complaint")
public class ComplaintController extends BaseController {

    private final ComplaintService complaintService;

    /**
     * 用户投诉列表（分页）
     */
    @SaCheckPermission("system:complaint:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<ComplaintVO> pageList(ComplaintDO dto, PageQuery pageQuery) {
        return complaintService.pageList(dto, pageQuery);
    }

    /**
     * 导出我要投诉
     */
    @Log(title = "我要投诉", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:complaint:export")
    @PostMapping("/export")
    public void export(ComplaintDO dto, HttpServletResponse response) {
        List<ComplaintVO> list = complaintService.selectComplaintList(dto);
        List<ComplaintExportVO> listVo = BeanUtil.copyToList(list, ComplaintExportVO.class);
        ExcelUtil.exportExcel(listVo, "投诉数据", ComplaintExportVO.class, response);
    }

    /**
     * 删除投诉
     */
    @SaCheckPermission("system:complaint:remove")
    @Log(title = "我要投诉", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(complaintService.deleteByIds(ids));
    }

    /**
     * 取详细信息
     * @param id
     */
    @SaCheckPermission("system:complaint:query")
    @GetMapping(value = "/{id}")
    public R<ComplaintVO> getInfo(@PathVariable Long id) {
        return R.ok(complaintService.getInfo(id));
    }

    /**
     * 分类-列表（分页）
     */
    @SaCheckPermission("system:categoryData:pageList")
    @GetMapping("/category/pageList")
    public TableDataInfo<ComplaintCategoryVO> categoryPageList(ComplaintCategoryDO dto, PageQuery pageQuery) {
        return complaintService.categoryPageList(dto, pageQuery);
    }

    /**
     * 分类-新增
     */
    @SaCheckPermission("system:categoryData:add")
    @Log(title = "我要投诉", businessType = BusinessType.INSERT)
    @PostMapping("/category")
    public R<Void> addCategory(@Validated @RequestBody ComplaintCategoryDO dto) {
        if (!complaintService.checkCategoryNameUnique(dto)) {
            return R.fail("新增投诉分类'" + dto.getName() + "'失败，投诉分类名称已存在");
        }
        return toAjax(complaintService.addCategory(dto));
    }

    /**
     * 分类-修改
     */
    @SaCheckPermission("system:categoryData:edit")
    @Log(title = "我要投诉", businessType = BusinessType.UPDATE)
    @PutMapping("/category")
    public R<Void> editCategory(@Validated @RequestBody ComplaintCategoryDO dto) {
        if (!complaintService.checkCategoryNameUnique(dto)) {
            return R.fail("修改投诉分类'" + dto.getName() + "'失败，投诉分类名称已存在");
        }
        return toAjax(complaintService.editCategory(dto));
    }

    /**
     * 分类-获取详情
     */
    @GetMapping("/category/{id}")
    public R<ComplaintCategoryVO> getCategoryInfo(@PathVariable Long id) {
        return R.ok(complaintService.getCategoryInfo(id));
    }

    /**
     * 分类-删除
     */
    @SaCheckPermission("system:categoryData:remove")
    @Log(title = "我要投诉", businessType = BusinessType.DELETE)
    @DeleteMapping("/category/{id}")
    public R<Void> removeCategory(@PathVariable Long id) {
        return toAjax(complaintService.removeCategory(id));
    }

    /**
     * 分类-获取详情
     */
    @GetMapping("/category/list")
    public R<List<ComplaintCategoryVO>> getCategoryList() {
        return R.ok(complaintService.getCategoryList());
    }

    /**
     * 导出pdf文件
     * @param response
     */
    @PostMapping("/pdf/export/{id}")
    public void registrationNoticeExport(@PathVariable Long id,HttpServletResponse response) throws UnsupportedEncodingException {
        ComplaintVO info = complaintService.getInfo(id);
        if (info == null) {
            throw new RuntimeException("投诉内容不存在！");
        }
        HashMap<String, String> dataMap = new HashMap<>();
        dataMap.put("id", Convert.toStr(id));
        dataMap.put("name",info.getName());
        dataMap.put("mobile",info.getMobile());
        dataMap.put("content",info.getContent());
        dataMap.put("type",info.getType());
        dataMap.put("email",info.getEmail());
        dataMap.put("enterprise",info.getEnterprise());
        dataMap.put("leadDept",info.getLeadDept());
        dataMap.put("auditDept",info.getAuditDept());
        dataMap.put("circulationType",info.getCirculationType());
        dataMap.put("timeLimit",info.getTimeLimit());
        //导出pdf
        PdfUtil.setResponseContentType(response, "投诉");
        PdfUtil.downloadPdf(dataMap,response);
    }

    /**
     * 流转、更新流转
     */
    @SaCheckPermission("system:complaint:circulation")
    @Log(title = "我要投诉", businessType = BusinessType.INSERT)
    @PostMapping("/circulation")
    public R<Void> circulation(@Validated @RequestBody ComplaintDO dto) {
        return toAjax(complaintService.circulation(dto));
    }
}
